package affiliation;

import java.util.Date;
import java.util.Hashtable;

import main.Paycheck;

import schedule.Period;
import serviceChargeTransaction.ServiceCharge;

public class UnionAffiliation implements Affiliation {
	private Hashtable<Date, ServiceCharge> charges = new Hashtable<Date, ServiceCharge>();
	private int memberId;
	private final double dues;

	public UnionAffiliation(int memberId, double dues) {
		this.memberId = memberId;
		this.dues = dues;
	}

	public UnionAffiliation() {
		this.memberId = -1;
		this.dues = 0.0;
	}

	public ServiceCharge getServiceCharge(Date time) {
		return (ServiceCharge) charges.get(time);
	}

	public void addServiceCharge(ServiceCharge sc) {
		charges.put(sc.getTime(), sc);
	}

	public double getDues() {
		return this.dues;
	}

	public int getMemberId() {
		return this.memberId;
	}

	public double calculateDeductions(Paycheck paycheck) {
		double totalDues = 0;
		int fridays = Period.getNumberOfFridaysInPayPeriod(
				paycheck.getPayPeriodStartDate(),
				paycheck.getPayPeriodEndDate());
		totalDues = dues * fridays;
		for (ServiceCharge charge : charges.values()) {
			if (Period.isInPayPeriod(charge.getTime(),
					paycheck.getPayPeriodStartDate(),
					paycheck.getPayPeriodEndDate())) {
				totalDues += charge.getAmount();
			}
		}
		
		return totalDues;
	}
}
